package castledefense.gui;

import twl.global.input.GlobalEvent;
import twl.global.widget.GlobalPanel;

import castledefense.data.GameData;
import castledefense.logic.effects.ActivatedAbilities;

public class SpellPanel extends GlobalPanel{		
	private GUIMainGame stateGui;
	
	private SpellButton fireSpell = new SpellButton("F1", GameData.fire, this);
	private SpellButton waterSpell = new SpellButton("F2", GameData.water, this);
	private SpellButton earthSpell = new SpellButton("F3", GameData.earth, this);
	private SpellButton airSpell = new SpellButton("F4", GameData.air, this);
	private ActivatedAbilities activatedAbilities;
	
	public final ActivatedAbilities getActivatedAbilities() {
		return activatedAbilities;
	}
	
	public SpellPanel(GUIMainGame gui, ActivatedAbilities aA){
		this.stateGui = gui;
		activatedAbilities = aA;
		
		setTheme("panelTheme");
		//Add building button
		add(fireSpell);
		fireSpell.setPosition(9+0*(fireSpell.getWidth()+9), 8);
		add(waterSpell);
		waterSpell.setPosition(9+1*(waterSpell.getWidth()+9),8);
		add(earthSpell);
		earthSpell.setPosition(9+2*(earthSpell.getWidth()+9), 8);
		add(airSpell);
		airSpell.setPosition(9+3*(airSpell.getWidth()+9), 8);
		
		stateGui.getTWL().getGlobalGUI().addGlobalEventListener(this);
	}
	
	public GUIMainGame getStateGUI(){
		return stateGui;
	}
	
	@Override
	public void handleGlobalEvent(GlobalEvent evt){
		if(evt.getType() == GlobalEvent.Type.KEY_PRESSED){
			int keyCode = evt.getKeyCode();
			//Fire
			if (keyCode == GlobalEvent.KEY_F1){
				activate(GameData.fire);				
			}
			//Water
			if (keyCode == GlobalEvent.KEY_F2) {
				activate(GameData.water);
			}
			//Earth
			if (keyCode == GlobalEvent.KEY_F3) {
				activate(GameData.earth);
			}
			//Air
			if (keyCode == GlobalEvent.KEY_F4) {
				activate(GameData.air);
			}
		}
	}
	
	public float getCooldownPercent(int type){
		return activatedAbilities.coolDownLeftAsFloat(type);
	}
	
	public void activate(int spellType){
		activatedAbilities.activated(spellType);
	}
}
